//
//  PSCarouselView.m
//  QklttiOS
//
//  Created by Vic on 2018/5/13.
//  Copyright © 2018年 Vic. All rights reserved.
//

#import "PSCarouselView.h"
#import "PSPageControl.h"

@interface PSCarouselView ()<UIScrollViewDelegate>
{
    UIScrollView *carouselScrollView;
    
    UIImageView *leftImageView;
    UIImageView *middleImageView;
    UIImageView *rightImageView;
    NSInteger currentIndex;
    PSPageControl *pageCtl;
}
@end

@implementation PSCarouselView

- (instancetype)initWithFrame:(CGRect)frame images:(NSArray *)images space:(CGFloat)space bgImage:(nullable NSString *)bgImage {
    self = [super initWithFrame:frame];
    if (self) {
        self.imagesArray = [NSMutableArray arrayWithCapacity:0];
        [self.imagesArray addObjectsFromArray:images];
        currentIndex = 0;
        
        [self createUISpace:space bgImage:bgImage];
        
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesClick)];
        [self addGestureRecognizer:tapGes];
    }
    return self;
}
- (void)createUISpace:(CGFloat)space bgImage:(nullable NSString *)bgImage {
    carouselScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    carouselScrollView.contentSize = CGSizeMake(carouselScrollView.frame.size.width*3, carouselScrollView.frame.size.height);
    carouselScrollView.contentOffset = CGPointMake(carouselScrollView.frame.size.width, carouselScrollView.frame.origin.y);
    carouselScrollView.backgroundColor = COLOR_OF_CLEAR;
    
    leftImageView = [UIToolsControl initImageViewWithFrame:CGRectMake(space, space, carouselScrollView.frame.size.width-space*2, carouselScrollView.frame.size.height-space*2) imageName:bgImage];
    leftImageView.layer.cornerRadius = space;
    leftImageView.layer.masksToBounds = YES;
    middleImageView = [UIToolsControl initImageViewWithFrame:CGRectMake(carouselScrollView.frame.size.width+space, space, carouselScrollView.frame.size.width-space*2, carouselScrollView.frame.size.height-space*2) imageName:bgImage];
    middleImageView.layer.cornerRadius = space;
    middleImageView.layer.masksToBounds = YES;
    rightImageView = [UIToolsControl initImageViewWithFrame:CGRectMake(carouselScrollView.frame.size.width*2+space, space, carouselScrollView.frame.size.width-space*2, carouselScrollView.frame.size.height-space*2) imageName:bgImage];
    rightImageView.layer.cornerRadius = space;
    rightImageView.layer.masksToBounds = YES;
    
//    self.leftImageView.contentMode = UIViewContentModeScaleAspectFit;
//    self.middleImageView.contentMode = UIViewContentModeScaleAspectFit;
//    self.rightImageView.contentMode = UIViewContentModeScaleAspectFit;
    
    
    leftImageView.backgroundColor = RGB(230, 225, 220);
    middleImageView.backgroundColor = RGB(230, 225, 220);
    rightImageView.backgroundColor = RGB(230, 225, 220);
    
    
    carouselScrollView.delegate = self;
    [carouselScrollView addSubview:leftImageView];
    [carouselScrollView addSubview:middleImageView];
    [carouselScrollView addSubview:rightImageView];
    carouselScrollView.showsVerticalScrollIndicator = NO;
    carouselScrollView.showsHorizontalScrollIndicator = NO;
    carouselScrollView.pagingEnabled = YES;
    carouselScrollView.userInteractionEnabled = YES;
    [self addSubview:carouselScrollView];
    
//    pageCtl = [[PSPageControl alloc] initWithFrame:CGRectMake(0, carouselScrollView.frame.size.height-25*Scale, carouselScrollView.frame.size.width, 20*Scale)];
    pageCtl = [[PSPageControl alloc] initWithFrame:CGRectMake(carouselScrollView.frame.size.width*0.5-(15*Scale+self.imagesArray.count*17*Scale)*0.5, carouselScrollView.frame.size.height-25*Scale, (15*Scale+self.imagesArray.count*17*Scale), 20*Scale)];
    pageCtl.pageIndicatorTintColor = RGBA(255, 255, 255, 0.7);
    pageCtl.currentPageIndicatorTintColor = COLOR_OF_WHITE;
    [self addSubview:pageCtl];
    
    if (self.imagesArray.count) {
        pageCtl.numberOfPages = self.imagesArray.count;
        [self setImage:currentIndex];
        [self createTimer];
    }
}
- (void)createTimer {
    self.timer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(timerClick) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)timerClick {
    currentIndex = (currentIndex+1+self.imagesArray.count)%self.imagesArray.count;
    pageCtl.currentPage = currentIndex;
    [carouselScrollView setContentOffset:CGPointMake(carouselScrollView.frame.size.width*2, 0) animated:YES];
    
    [self setImage:currentIndex];
    
    carouselScrollView.contentOffset = CGPointMake(carouselScrollView.frame.origin.x, carouselScrollView.frame.origin.y);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.timer invalidate];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    [self createTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = carouselScrollView.contentOffset.x;
    if (offsetX == carouselScrollView.frame.size.width*2) {
        currentIndex = (currentIndex+1+self.imagesArray.count)%self.imagesArray.count;
    } else if (offsetX == 0.0) {
        currentIndex = (currentIndex-1+self.imagesArray.count)%self.imagesArray.count;
    } else {
        return;
    }
    
    pageCtl.currentPage = currentIndex;
    if (self.imagesArray.count) {
        [self setImage:currentIndex];
    }
    carouselScrollView.contentOffset = CGPointMake(carouselScrollView.frame.size.width, 0);
}
- (void)setImage:(NSInteger)index {
    NSInteger middleIndex = (index+self.imagesArray.count)%self.imagesArray.count;
    middleImageView.image = [UIImage imageNamed:self.imagesArray[middleIndex]];
    
    NSInteger leftIndex = (index-1+self.imagesArray.count)%self.imagesArray.count;
    leftImageView.image = [UIImage imageNamed:self.imagesArray[leftIndex]];
    
    NSInteger rightIndex = (index+1+self.imagesArray.count)%self.imagesArray.count;
    rightImageView.image = [UIImage imageNamed:self.imagesArray[rightIndex]];
    
    /*
    NSInteger middleIndex = (index+self.imagesArray.count)%self.imagesArray.count;
    BannerModel *middleModel = (BannerModel *)self.imagesArray[middleIndex];
    [middleImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", IMAGE_URL, middleModel.picUrl]] placeholderImage:[UIImage imageNamed:@"banner_bg"]];

    NSInteger leftIndex = (index-1+self.imagesArray.count)%self.imagesArray.count;
    BannerModel *leftModel = (BannerModel *)self.imagesArray[leftIndex];
    [leftImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", IMAGE_URL, leftModel.picUrl]] placeholderImage:[UIImage imageNamed:@"banner_bg"]];

    NSInteger rightIndex = (index+1+self.imagesArray.count)%self.imagesArray.count;
    BannerModel *rightModel = (BannerModel *)self.imagesArray[rightIndex];
    [rightImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", IMAGE_URL, rightModel.picUrl]] placeholderImage:[UIImage imageNamed:@"banner_bg"]];
    */
}
- (void)tapGesClick {
//    BannerModel *model = self.imagesArray[currentIndex];
//    self.tapImage(model);
    self.tapImage();
}

@end
